package cmd

import (
	"fmt"
	"svr/src/conf"
	"svr/src/msg"
	"svr/src/ws"
	"time"

	"google.golang.org/protobuf/proto"
)

func init() {
	ws.AddHandler(&msg.Ping{}, handlerPing)
	ws.AddHandler(&msg.Pong{}, handlerPong)
	fmt.Println("-=-=-=-=-=-=-=-")
}

func handlerPing(mr proto.Message, task *ws.MsgTask) *msg.Error {
	msgR := mr.(*msg.Ping)
	if conf.ServerConfig.Balance == msg.BalanceType_Alloc && task.Dev.Blance == msg.BalanceType_Upload {
		task.Dev.ClientCount = msgR.ParamNum
	}
	return task.ReturnMsg(&msg.Pong{})
}

func handlerPong(mr proto.Message, task *ws.MsgTask) *msg.Error {
	time.AfterFunc(ws.DurHeartWait, func() {
		m := &msg.Ping{}
		if conf.ServerConfig.Balance == msg.BalanceType_Upload && task.Dev.Blance == msg.BalanceType_Alloc {
			m.ParamNum = ws.GetBalanceCount()
		}
		task.ReturnMsg(m)
	})
	return nil
}

// 发现无响应服务器通知top，top测试无响应后，通知其他所有服
